package com.iwuyc.tools.commons.util;

/**
 * Url相关工具类
 *
 * @author Neil
 */
public class UrlUtils {
    private static final char OBLIQUE_LINE = '/';
    private static final String SCHEMA_SPLIT = "://";
    private UrlUtils() {
    }

    /**
     * 规范输出address的路径。<br>
     * <pre>
     * Example:
     * 1、https://www.iwuyc.com : https://www.iwuyc.com
     * 2、https://www.iwuyc.com/ : https://www.iwuyc.com
     * 3、https://www.iwuyc.com/// : https://www.iwuyc.com
     * 4、https://www.iwuyc.com/context-path// : https://www.iwuyc.com/context-path
     * </pre>
     *
     * @param address 待处理的地址
     * @return 规范化后的路径
     */
    public static String addressFix(String address) {
        final int colonIndex = address.indexOf(SCHEMA_SPLIT);
        StringBuilder addressBuilder = new StringBuilder(address.length());
        final int splitIndex;
        if (colonIndex > 0) {
            addressBuilder.append(address, 0, colonIndex + SCHEMA_SPLIT.length());
            splitIndex = colonIndex + SCHEMA_SPLIT.length();
        } else {
            splitIndex = 0;
        }
        final String obliqueLineFix = obliqueLineFix(address.substring(splitIndex));
        final int obliqueLineLength = obliqueLineFix.length();
        if (obliqueLineFix.charAt(obliqueLineLength - 1) == OBLIQUE_LINE) {
            addressBuilder.append(obliqueLineFix, 0, obliqueLineLength - 1);
        } else {
            addressBuilder.append(obliqueLineFix);
        }
        return addressBuilder.toString();
    }

    /**
     * 规范输出path的路径。<br>
     * <pre>
     * Example:
     * 1、/product/list : /product/list
     * 2、product/list : /product/list
     * 3、//product/list : /product/list
     * 4、/product/list/ : /product/list/
     * 4、///product///list/// : /product/list/
     * 5、/product/list : /product/list
     * 5、///product////list : /product/list
     * </pre>
     *
     * @param path 待处理的path路径
     * @return 规范化后的路径
     */
    public static String pathFix(String path) {
        final String obliqueLineFix = obliqueLineFix(path);
        final char firstChar = obliqueLineFix.charAt(0);
        if (firstChar == OBLIQUE_LINE) {
            return obliqueLineFix;
        }
        return OBLIQUE_LINE + obliqueLineFix;
    }

    /**
     * 去除多余的斜线
     *
     * @param str 待处理的字符串
     * @return 处理后的字符串
     */
    private static String obliqueLineFix(String str) {
        final int length = str.length();
        StringBuilder pathBuilder = new StringBuilder(length);
        boolean lastIsObliqueLine = false;
        for (int i = 0; i < length; i++) {
            final char charCursor = str.charAt(i);
            if (OBLIQUE_LINE != charCursor) {
                pathBuilder.append(charCursor);
                lastIsObliqueLine = false;
            } else if (!lastIsObliqueLine) {
                pathBuilder.append(OBLIQUE_LINE);
                lastIsObliqueLine = true;
            }
        }

        return pathBuilder.toString();
    }

    /**
     * 处理url
     *
     * @param address 目标地址
     * @param path    目标机器上的资源路径
     * @return 统一规范处理后的URL地址
     */
    public static String urlFix(String address, String path) {
        return addressFix(address) + pathFix(path);
    }
}
